package security.access;

import java.util.Hashtable;

public class AccessCheckerFactory {
	private static AccessCheckerFactory instance;
	
	private AccessChecker recovered;
	private AccessChecker repository;
	private AccessChecker backup;
	
	private AccessCheckerFactory(String repository, 
			String recovered, String[] backup){
		
		Hashtable<String,Integer> rights = new Hashtable<String,Integer>();
		
		rights.put(recovered, AccessChecker.LIST+AccessChecker.WRITE);
		this.recovered = new AccessCheckerImpl(rights,false);
		
		rights.clear();
		rights.put(repository, AccessChecker.LIST+AccessChecker.WRITE+AccessChecker.READ);
		this.repository = new AccessCheckerImpl(rights,false);
		
		rights.clear();
		for(String dir : backup)
			rights.put(dir, AccessChecker.LIST+AccessChecker.READ);
		this.backup = new AccessCheckerImpl(rights,true);
	}
	
	public static void register(String repository, 
			String recovered, String[] backup){
		if(instance != null)
			throw new RuntimeException("A manager has already been registred.");
		
		instance = new AccessCheckerFactory(repository,recovered,backup);
	}
	
	public static AccessCheckerFactory getInstance(){
		if(instance == null)
			throw new RuntimeException("No manager has been registred yet.");
		return instance;
	}
	
	public static AccessChecker getRecoveredAccess(){
		return getInstance().recovered;
	}
	
	public static AccessChecker getBackupAccess(){
		return getInstance().backup;
	}
	
	public static AccessChecker getRepositoryAccess(){
		return getInstance().repository;
	}
}
